package com.jltx.zeus.observable

import io.reactivex.*

/**
 * Call到Observable的转换
 * 及Observable到其他RxJava可观察对象的转换
 */
object CallObservableAdapter {
    /**
     * 将AsyncCall转换为Observable
     *
     * @param call
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> adapt(call: AsyncCall<T>): Observable<T> {
        return AsyncCallObservable(call)
    }

    /**
     * 将AsyncMultiCall转换为Observable
     *
     * @param call
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> adapt(call: AsyncMultiCall<T>): Observable<T> {
        return AsyncMultiCallObservable(call)
    }

    /**
     * 将SyncCall转换为Observable
     *
     * @param call
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> adapt(call: SyncCall<T>): Observable<T> {
        return SyncCallObservable(call)
    }

    /**
     * 将Observable转换为Flowable
     * @param observable
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> toFlowable(observable: Observable<T>): Flowable<T> {
        return observable.toFlowable(BackpressureStrategy.LATEST)
    }

    /**
     * 将Observable转换为Flowable
     * @param observable
     * @param strategy 背压策略
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> toFlowable(
        observable: Observable<T>,
        strategy: BackpressureStrategy?
    ): Flowable<T> {
        return observable.toFlowable(strategy)
    }

    /**
     * 将Observable转换为Single
     * @param observable
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> toSingle(observable: Observable<T>): Single<T> {
        return observable.singleOrError()
    }

    /**
     * 将Observable转换为Maybe
     * @param observable
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> toMaybe(observable: Observable<T>): Maybe<T> {
        return observable.singleElement()
    }

    /**
     * 将Observable转换为Completable
     * @param observable
     * @param <T>
     * @return
    </T> */
    @JvmStatic
    fun <T> toCompletable(observable: Observable<T>): Completable {
        return observable.ignoreElements()
    }
}
